/*
 * Copyright 2011-2011 Tiago da Fonseca Frazao  All rights reserved.
 */

package mz.co.research.shared.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * @author Tiago da Fonseca Frazao
 */
@Entity
@Table(name = "QUESTION")
public class Question extends UpdatableEntity {

	@NotNull
	@Size(max = 255)
	@Column(name = "DESCRIPTION", length = 255, nullable = true)
	private String description;

	@OneToOne
	@JoinColumn(name = "FK_ANSWER_TYPE_ID", nullable = false)
	private AnswerType answerType;

	@OneToOne(cascade = CascadeType.PERSIST)
	@JoinColumn(name = "FK_ANSWER_DOMAIN_ID", nullable = true)
	private AnswerDomain answerDomain;

	@OneToMany(mappedBy = "question", cascade = CascadeType.PERSIST)
	private List<Answer> answers = new ArrayList<Answer>();

	@ManyToOne
	@JoinColumn(name = "FK_FORM_ID", nullable = false)
	private Form form;

	/**
	 * @return the description
	 */
	public String getDescription() {
		return description;
	}

	/**
	 * @param description
	 *            the description to set
	 */
	public void setDescription(String description) {
		this.description = description;
	}

	/**
	 * @param the
	 *            answer to add
	 */
	public void addAnswer(Answer answer) {
		answer.setQuestion(this);
		this.answers.add(answer);
	}

	/**
	 * @param answer
	 *            the answer to remove
	 */
	public void removeAnswer(Answer answer) {
		this.answers.remove(answer);
	}

	/**
	 * @return the answerType
	 */
	public AnswerType getAnswerType() {
		return answerType;
	}

	/**
	 * @param answerType
	 *            the answerType to set
	 */
	public void setAnswerType(AnswerType answerType) {
		this.answerType = answerType;
	}

	/**
	 * @return the answerDomain
	 */
	public AnswerDomain getAnswerDomain() {
		return answerDomain;
	}

	/**
	 * @param answerDomain
	 *            the answerDomain to set
	 */
	public void setAnswerDomain(AnswerDomain answerDomain) {
		this.answerDomain = answerDomain;
	}

	/**
	 * @return the form
	 */
	public Form getForm() {
		return form;
	}

	/**
	 * @param form
	 *            the form to set
	 */
	public void setForm(Form form) {
		this.form = form;
	}

	@Override
	public String toString() {
		StringBuilder buffer = new StringBuilder("\nQuestion");

		buffer.append(super.toString());

		buffer.append("\ndescription :" + this.description);

		return buffer.toString();
	}
}
